package com.example8.rabbitmq_module.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description
 * @Author lihw
 * @Date 2024/1/23 14:22
 */
@Slf4j
@Configuration
@Data
public class RabbitMQConfig {

    private final String EXCHANGE = "lhw.exchange.topic";

    private final String QUEUE = "lhw.queue.topic";

    private final String ROUTING_KEY = "lhw.routing.key1";

    private final String BINDING_KEY = "lhw.routing.*";

    @Bean
    public TopicExchange lhwTopicExchange() {
        return new TopicExchange(EXCHANGE, true, false);
    }

    @Bean
    public Queue lhwQueue() {
        // exclusive: 独占性。如果设置为true，这个队列将只能被声明它的连接使用，其他连接不能访问。当连接关闭时，队列将被自动删除。这对于只限于该连接的临时队列很有用
        return new Queue(QUEUE, true, false, false);
    }

    @Bean
    public Binding lhwBinding() {
        return BindingBuilder.bind(lhwQueue()).to(lhwTopicExchange()).with(BINDING_KEY);
    }

}
